<?php
namespace app\gacj\home;
use app\index\controller\Home;
use think\Db;
use thans\jwt\facade\JWTAuth;
use app\gacj\model\User as UserModel;

/**
 * 前台公共控制器
 * @package app\cms\admin
 */
class Common extends Home
{
    public $user;

    /**
     * 初始化方法
     * @author rocky
     */
    public function initialize()
    {
        parent::initialize();

        /*
        $user_id = $this->auth();
        if($user = UserModel::where('id', $user_id)->find()){
            $this->user = $user;
        }else{
            headError(406);
            jerror('授权失败');
        }
        */
    }

    protected function auth()
    {
        try {
            $payload = JWTAuth::auth();
            return $payload['id']->getValue();
        } catch (\thans\jwt\exception\TokenExpiredException $e) {
            //过期 续期使用
            headError(402);
            jerror($e->getMessage());
        } catch (\thans\jwt\exception\TokenBlacklistException $e) {
            //黑名单 无法使用
            headError(403);
            jerror($e->getMessage());
        } catch (\Exception $e) {
            //失败 重新授权
            headError(401);
            jerror($e->getMessage());
        }
    }
}